package com.hngy.educationaladministration.utils;

import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

/**
 * @author Deng JunHao
 * @date 2021/05/20 15:17
 * @description：
 **/
public class MD5Util {

    public MD5Util() {
    }

    /**
     * 生成32位加密md5
     *
     * @param str
     * @return
     */
    public static String encrypt32(String str) {
        if (str == null) {
            return null;
        } else {
            String result = "";

            try {
                MessageDigest md = MessageDigest.getInstance("MD5");
                md.update(str.getBytes("utf-8"));
                byte[] b = md.digest();
                StringBuffer buf = new StringBuffer("");

                for (int offset = 0; offset < b.length; ++offset) {
                    int i = b[offset];
                    if (i < 0) {
                        i += 256;
                    }

                    if (i < 16) {
                        buf.append("0");
                    }

                    buf.append(Integer.toHexString(i));
                }

                result = buf.toString();
            } catch (NoSuchAlgorithmException var7) {
                var7.printStackTrace();
            } catch (UnsupportedEncodingException var8) {
                var8.printStackTrace();
            }

            return result;
        }
    }

    public static String encrypt16(String str) {
        return str == null ? null : encrypt32(str).substring(8, 24);
    }
}
